<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['module:wpType:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:wpType:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:wpType:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-divider/>

    <el-row :gutter="20">
      <el-form v-loading="loading" @change="handleSelectionChange" :model="form" :rules="rules" >
        <div class="mt-1 pd-2">
          <el-checkbox-group v-for="item in wpTypeList" v-model="ids" size="large">
            <el-col>
              <div class="pd-2">
                <el-checkbox 
                  v-model="item.id" 
                  :key="item.id" 
                  :label="item.id" 
                  border 
                  >{{ item.name }}</el-checkbox>
              </div>
            </el-col>
          </el-checkbox-group>
        </div>
      </el-form>
    </el-row>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改壁纸类型对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="wpTypeRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="请输入类型" prop="name">
              <el-input v-model="form.name" type="textarea" placeholder="请输入类型，多个类型请用 “空格” 或 “逗号” 隔开" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="WpType">
import { listWpType, getWpType, delWpType, addWpType, updateWpType } from "@/api/module/wpType";

const { proxy } = getCurrentInstance();

const wpTypeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 20,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询壁纸类型列表 */
function getList() {
  loading.value = true;
  listWpType(queryParams.value).then(response => {
    wpTypeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
  single.value = true;
  multiple.value = true;
  ids.value = [];
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: []
  };
  proxy.resetForm("wpTypeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange() {
  single.value = ids.value.length != 1;
  multiple.value = !ids.value.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加壁纸类型";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getWpType(_id).then(response => {
    form.value = response.data;
    form.value.name = form.value.name.split(",");
    open.value = true;
    title.value = "修改壁纸类型";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["wpTypeRef"].validate(valid => {
    if (valid) {
      // form.value.name = form.value.name.join(",");
      if (form.value.id != null) {
        updateWpType(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addWpType(form.value).then(response => {

          // 提示语没有重复提示添加成功，有重复则提示重复项
          if(response.data.data.length == 0){
            proxy.$modal.msgSuccess("操作成功！");
          }else{
            proxy.$modal.msgSuccess("操作成功，但以下类型重复，已自动过滤: " + response.data.data);
          }
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除该项壁纸类型？').then(function() {
    return delWpType(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('module/wpType/export', {
    ...queryParams.value
  }, `wpType_${new Date().getTime()}.xlsx`)
}

getList();
</script>
<style>
.mt-1{
   display:flex;
}
.pd-2{
  padding: 2px 2px;
  flex-wrap:wrap;
}
</style>